package com.haoshuang.zhouzhoubang.ui.page.mine.wallet.frozen.detail

import android.content.Context
import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.haoshuang.zhouzhoubang.databinding.ActivityFrozenAmountDetailBinding
import com.haoshuang.zhouzhoubang.model.bean.CyBean
import com.haoshuang.zhouzhoubang.ui.activity.ServiceItemAdapter
import com.haoshuang.zhouzhoubang.ui.dialog.JfDialog
import com.lxj.xpopup.XPopup
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.startPage
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click
import com.mufeng.libs.utils.view.clickWithTrigger
import com.mufeng.libs.utils.view.gone
import com.mufeng.libs.utils.view.visible
import com.mufeng.libs.utils.view.visibleOrGone
import java.math.BigDecimal

/**
 *  冻结金额明细
 */
class FrozenAmountDetailActivity :
    BaseVMActivity<FrozenAmountDetailViewModel, ActivityFrozenAmountDetailBinding>() {

    companion object {
        fun launch(context: Context) {
            context.startPage<FrozenAmountDetailActivity>()
        }
    }

    lateinit var adapter: DyAdapter
    var list = arrayListOf<CyBean>()

    override fun initView(savedInstanceState: Bundle?) {
        binding.titleBar.title = "冻结金额明细"
        binding.titleBar.leftImageView().click { finish() }

        adapter = DyAdapter(list)
        var ids = intent.getIntExtra("ids", 0)
        var is_team = intent.getIntExtra("is_team", 0)

        var is_deposit = intent.getStringExtra("is_deposit")
        if (is_deposit == "3") {
            is_deposit = "0"
        }

        var deposit_tip = intent.getStringExtra("deposit_tip")
        var money = intent.getStringExtra("money")
        var bd = BigDecimal(money)
        var value = bd.setScale(2, BigDecimal.ROUND_HALF_UP)
        binding.orderTotal.text = value.toString()
        vm.getDepositDetail(ids.toString())
        if (is_deposit == "0") {
            binding.llTh.gone()
        } else {
            binding.llTh.visible()
        }
        if (is_team == 0) {
            binding.llTd.gone()
        } else {
            binding.llTd.visible()
        }
        binding.apply {
            val layoutManager = FlexboxLayoutManager(mActivity)
            layoutManager.flexDirection = FlexDirection.ROW_REVERSE
            layoutManager.flexWrap = FlexWrap.WRAP
            rvList.layoutManager = layoutManager
            rvList.adapter = adapter

            tvJf.clickWithTrigger {
                XPopup.Builder(this@FrozenAmountDetailActivity)
                    .asCustom(JfDialog(this@FrozenAmountDetailActivity) {

                    })
                    .show()
            }
        }
    }

    override fun initData() {
    }

    override fun startObserve() {
        vm.getDepositDetailData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                val it = it?.data
                dismissLoading()
                binding.apply {

                    costTotal.text = it?.cost_total
                    refundTotal.text = "-" + it?.refund_total
                    masterTotal.text = "+" + it?.master_total
                    serviceTotal.text = it?.service_total
                    payTotal.text = it?.pay_total
                    payTotal2.text = it?.real_name
                    tvInfo.text = it?.team_freeze_tip
                    if (!it?.team_member_total.isNullOrBlank()) {
                        teamMemberTotal.text = it?.team_member_total
                        llDd.visible()
                    } else {
                        teamMemberTotal.text = it?.team_member_total
                        llDd.gone()
                    }

//                    serviceTotal2.text=it?.store_name

                    if (it?.is_cart_order == 1) {
                        binding.tvServiceNum.text = "共" + it?.order_goods?.size + "个服务项"
                        binding?.storeName?.visibility = View.GONE
                        binding.recyclerViewSeriviceItem.visibility = View.VISIBLE
                        if (!it?.order_goods.isNullOrEmpty()) {
                            binding?.tvServiceNum?.text =
                                "共" + it?.order_goods?.size + "项服务明细"
                            var serviceItemAdapter = ServiceItemAdapter(it?.order_goods!!)
                            binding.recyclerViewSeriviceItem.layoutManager = LinearLayoutManager(
                                this@FrozenAmountDetailActivity,
                                LinearLayoutManager.VERTICAL, false
                            )
                            binding.recyclerViewSeriviceItem.adapter = serviceItemAdapter
                        }
                        binding.storeName.visibility = View.GONE

                    } else {
                        binding.storeName.text = it?.store_name
                        binding.recyclerViewSeriviceItem.visibility = View.GONE
                        binding?.tvServiceNum?.visibility = View.GONE
                    }

                    tvTime.text = it?.confirm_service_time
                    tvTime2.text = it?.order_id
                    list.addAll(it?.team_member!!.filter { it.order_id != 0 })
                    adapter.notifyDataSetChanged()
                    platformTotal.text = "（用户实付：${it?.pay_total}，平台补贴${it?.platform_total}）"
                }
                val baoXianPrice = it?.deduct_insurance_amount?.toDoubleOrNull() ?: 0.0
                binding.llBaoXian.visibleOrGone(baoXianPrice > 0.0)
                binding.tvBaoXianPrice.text = "-${it?.deduct_insurance_amount}"
            }
        }
    }

}